package vn.gq.udv.template;

import vn.gq.udv.utils.ButtonHelper;
import vn.gq.udv.utils.SharePreferenceUtil;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;

public class IntroActivity extends SherlockActivity {

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.firstscr);
		String author = getString(R.string.author_name);
		if (!author.trim().equals(""))
			getSupportActionBar().setSubtitle(author);
		Button btnExit = (Button) findViewById(R.id.btnExit);
		btnExit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				exit();
			}
		});
		
		Button btnMoreApp = (Button) findViewById(R.id.btnStore);
		btnMoreApp.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				new ButtonHelper(IntroActivity.this).showMoreApp();
			}
		});
		
		Button btnRead = (Button) findViewById(R.id.btnRead);
		btnRead.setText(GQConst.TYPE_BOOK ? "Đọc Sách" : "Đọc Truyện");
		btnRead.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (GQConst.SHOW_CATEGORY && SharePreferenceUtil.getInstance(IntroActivity.this).getBoolean(CategoryActivity.PREF_STARTCATEGORYACTIVITY, true))
					startActivity(new Intent(IntroActivity.this, CategoryActivity.class));
				else
					startActivity(new Intent(IntroActivity.this, ChapterActivity.class));
			}
		});
		
		Button btnContact = (Button) findViewById(R.id.btnContact);
		btnContact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_SENDTO);
				Uri uri = Uri.parse(GQConst.EMAIL);
				intent.setData(uri);
				startActivity(Intent.createChooser(intent, "Send mail..."));
			}
		});
	}

	@Override
	public void onBackPressed() {
	}

	public void exit() {
		AlertDialog.Builder builder = new AlertDialog.Builder(IntroActivity.this);
		builder.setMessage("Hãy dành chút thời gian đánh giá cho ứng dụng!")
		.setCancelable(true)
		.setTitle("ỨNG DỤNG VIỆT")
		.setIcon(android.R.drawable.btn_star)
		.setPositiveButton("Đánh giá", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				try {
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(GQConst.DETAILURI)));
				} catch(ActivityNotFoundException e) {
					Toast.makeText(IntroActivity.this, "Ứng dụng Google Play không được cài trên máy", Toast.LENGTH_SHORT).show();
				}
			}
		})
		.setNegativeButton("Thoát", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				onDestroy();
				System.runFinalizersOnExit(true);
				System.exit(0);
			}
		});
		AlertDialog alertDialog = builder.create();
		alertDialog.show();
	}

}
